package com.shengruan.admin.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.shengruan.admin.vo.Admin;
import com.shengruan.common.Constants;
import com.shengruan.framework.jdbc.BaseJdbc;
import com.shengruan.framework.jdbc.BeanHandler;
import com.shengruan.framework.jdbc.BeanListHandler;

public class AdminJdbc extends BaseJdbc{

	/**
	 * 保存
	 * @param admin
	 */
	public void save(Admin admin)
	{
		Connection con = this.getConnection();
		
		String sql = "insert into admin (username, password, creationTime, status, logins, email, mobile) values(?,?,?,?,0,?,?)";
		try {
			Object [] pram = {admin.getUsername(),admin.getPassword(),System.currentTimeMillis(),Constants.STATUS_ACTIVE,admin.getEmail(),admin.getMobile()};
			this.execute(con, sql, pram);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(con);
		}
	}
	/**
	 * 根据用户名获得用户
	 *
	 * @param userName
	 * @return
	 */
	public Admin getByAdminName(String userName) {
		Connection conn = this.getConnection();
		String sql = "select * from admin where username=?";
		Admin admin = null;
		try
		{
			admin = this.query(conn, sql, new BeanHandler<Admin>(Admin.class),new Object[]{userName});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
		return admin;
	}


	/**
	 * 修改管理员信息
	 *
	 * @param admin
	 */
	public void updateAdmin(Admin admin) {
		Connection conn = this.getConnection();
		StringBuilder sql = new StringBuilder("update admin set lastloginTime = ?,lastloginIp = ?, logins = ? where id = ?");
		try {
			Object [] pram = {admin.getLastloginTime(),admin.getLastloginIp(),admin.getLogins(),admin.getId()};
			this.execute(conn, sql.toString(),pram);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
	}


	public int getRows()
	{
		return 0 ;
	}

	/***
	 * 获得用户列表
	 * @param pageSize
	 * @param currentPage
	 * @return
	 */
	public List<Admin> findWithPage(int currentPage,int pageSize){
		Connection conn = this.getConnection();
		String sql = "select * from admin order by creationTime desc limit ?,?";
		List<Admin> list = null;
		try
		{
			int start = (currentPage-1)*pageSize;
			list = this.query(conn, sql, new BeanListHandler<Admin>(Admin.class),new Object[]{start,pageSize});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
		return list;
	}




	/***
	 * 根据用户ID删除
	 */
	public void deleteById(String id){
		Connection conn = this.getConnection();
		String sql = "delete from admin where id = ? ";
		try {
			this.execute(conn, sql,new Object[]{id});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
	}

	/**
	 * 修改状态
	 */
	public void updateStatus(String id,String status){
		Connection conn = this.getConnection();

		try {
			String sql = "update admin set status = ? where id = ?";
			this.execute(conn, sql,new Object[]{status,id});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
	}

	/**
	 * 修改管理员的手机和Email
	 * @param id
	 * @param mobile
	 * @param email
	 */
	public void updateMobileAndEmail(String id,String mobile, String email){
		Connection conn = this.getConnection();
		try {
			String sql = "update admin set mobile = ?, email = ? where id = ?";
			this.execute(conn, sql, new Object[]{mobile,email,id});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
	}


	/**
	 * 修改密码
	 */
	public void changePassword(String id,String password){
		Connection conn = this.getConnection();

		try {
			String sql = "update admin set password = ? where ID = ?";
			this.execute(conn, sql,new Object[]{password,id});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
	}


	/***
	 * 根据ID获得Admin信息
	 * @param id
	 * @return
	 */
	public Admin getById(String id)
	{
		Connection conn = this.getConnection();
		String sql = "select * from admin where id=?";
		Admin admin = null;
		try
		{
			admin = this.query(conn, sql, new BeanHandler<Admin>(Admin.class),new Object[]{id});
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(conn);
		}
		return admin;
	}
}
